import { mqttCentre } from '#/utils/mqttCentre.js';

export const env = CLIENT_ENV; //环境变量

// MQTT
export const MQTT_SHORT = {
  GRAB: `${env}-ultra-short-data-grab`,
  ISSUED: `${env}-ultra-short-data-issue`,
};

/** mqtt主题订阅
 * list：定义的主题集合  [{ key: MQTT_ENUM.TRANSACTION_ADD_CONTRACT, value: "" }]
 * callBack: 接口返回的数据处理
 */
export const clientMqtt = (list = [], callBack = () => {}, prefix = '') => {
  list.forEach((v) => {
    const { key, value } = v;
    const url = `${prefix}${key}`;
    mqttCentre.subscribe(url, `${env || ''}${value ? '/' : ''}${value || ''}`, callBack);
  });
};

// mqtt取消订阅
export const clearMqtt = (list = [], prefix = '') => {
  list.forEach((v) => {
    mqttCentre.unsubscribe(`${prefix}${v?.key}`);
  });
};
